<?php

function load_config()			
{
	$arr = array();
	
	$data = read_static_cache('shop_config');							//读取商城参数缓存 
	if($data == false)													//读取缓存失败,转向数据库读取
	{
		$sql = 'SELECT code,value FROM '. $GLOBALS['ecs']->table('shop_config').' WHERE parent_id>0';
		$res = $GLOBALS['db']->getAll($sql);
		
		foreach($res AS $row)
		{
			$arr[$row['code']] = $row['value'];
		}
		
		if(!isset($GLOBALS['_CFG']['ecs_version']))
		{
			$GLOBALS['_CFG']['ecs_version'] = 'v2.0.5';
		}
		
		$lang_array = array('zh_cn','zh_tw','en_us');
		if(empty($arr['lang']) || !in_array($arr['lang'], $lang_array))
		{
			$arr['lang'] = 'zh_cn';
		}
		
		write_static_cache('shop_config',$arr);						//将数据库读出的商城参数写入文件缓存
	}
	else 
	{
		$arr = $data;
	}
	
	return $arr;
}

/**
 *	初始化用户对象 
 */

function &init_users()											
{
	$set_modules = false;
	static $cls = null;
	if($cls != null)
	{
		return $cls;
	}
	include_once(ROOT_PATH. 'includes/modules/integrates/'. $GLOBALS['_CFG']['integrate_code'].'.php');		//加载整合程序模块
	$cfg = unserialize($GLOBALS['_CFG']['integrate_config']);
	$cls = new $GLOBALS['_CFG']['integrate_code']($cfg);				//根据参数实例化ecshop对象
	 
	return $cls;
}

function price_format($price,$change_price = true)
{
	if($change_price && defined('ECS_ADMIN') === false)
	{
		switch($GLOBALS['_CFG']['price_format'])
		{
			case 0:
				$price = number_format($price,2,'.','');
				break;
			case 1:
				$price = preg_replace('/(.*)(\\.)([0-9]*?)0+$/','\1\2\3', number_format($price,2,'.',''));
				
				if(substr($price, -1)=='.')
				{
					$price = substr($price, 0,-1);
				}
				break;
			case 2:
				$price = substr(number_format($price,2,'.',''), 0,-1);
				break;
			case 3:
				$price = intval($price);
				break;
			case 4:
				$price = number_format($price,1,'.','');
				break;
			case 5:
				$price = round($price);
				break;
		}
	}
	else 
	{
		$price = number_format($price,2,'.','');
	}
	
	return sprintf($GLOBALS['_CFG']['currency_format'],$price);
}



